#include "cellclustermarchingcube.h"
#include "graphics.opengl/renderer.h"
#include "graphics/graphicsmanager.h"

#include "planetcache.h"

namespace infinity {
	namespace render {

//CellClusterMarchingCube::CellClusterMarchingCube(int _size, Vector3i _origin )
//:	CellCluster(_size,_origin)
//{
//}
//
//void CellClusterMarchingCube::initializeMesh()
//{
//	dw::graphics::opengl::Renderer& renderer(static_cast< dw::graphics::opengl::Renderer& >(dw::graphics::GraphicsManager::getInstance().getRenderer()));
//
//	using dw::graphics::VertexElement;
//
//	static const VertexElement elements[] = {
//		{ VertexElement::vec3, VertexElement::position, 0 },
//		{ VertexElement::vec3, VertexElement::normal, 0 },
//		{ VertexElement::vec3, VertexElement::color, 0 }
//	};
//
//	m_vertexFormat = new dw::graphics::VertexFormat(elements, 3);
//
//	dw::int32 totalCube = size * size * size;
//	dw::int32 maxCubeCount = (totalCube + 1) >> 1;
//	dw::int32 maxQuadCount = 6 * maxCubeCount;
//	dw::int32 maxVerticesCount = 4 * maxQuadCount;
//	dw::int32 maxIndicesCount = 2 * 3 * maxQuadCount;
//
//	vertices = new dw::float32[maxVerticesCount * 3 * 3];
//	indices = new dw::uint16[maxIndicesCount];
//
//	curindex = 0;
//	curvertex = 0;
//
//	WorldGridManager& worldManager(WorldGridManager::getInstance());
//
//	Planet::Cell cell;
//	Planet::Cell neighbour;
//
//	Vector3i cacheOrigin = Vector3i(origin.x, origin.y, origin.z) - Vector3i(1, 1, 1); // prefetch neighbors too
//	Vector3i cacheDimensions = Vector3i(size + 2, size + 2, size + 2); // prefetch neighbors too
//
//	Planet::Cache cache(cacheDimensions);
//
//	cache.prefetch(worldManager.getPlanet(), cacheOrigin, cacheDimensions);
//
//	static const Vector3i Neighbours[3*3*3 - 1] = {
//		Vector3i(  1,  1,  1 ),
//		Vector3i(  1,  1,  0 ),
//		Vector3i(  1,  1, -1 ),
//		Vector3i(  1,  0,  1 ),
//		Vector3i(  1,  0,  0 ),
//		Vector3i(  1,  0, -1 ),
//		Vector3i(  1, -1,  1 ),
//		Vector3i(  1, -1,  0 ),
//		Vector3i(  1, -1, -1 ),
//		Vector3i(  0,  1,  1 ),
//		Vector3i(  0,  1,  0 ),
//		Vector3i(  0,  1, -1 ),
//		Vector3i(  0,  0,  1 ),
//		//Vector3i(  0,  0,  0 ), // c'est moi ici :D 
//		Vector3i(  0,  0, -1 ),
//		Vector3i(  0, -1,  1 ),
//		Vector3i(  0, -1,  0 ),
//		Vector3i(  0, -1, -1 ),
//		Vector3i( -1,  1,  1 ),
//		Vector3i( -1,  1,  0 ),
//		Vector3i( -1,  1, -1 ),
//		Vector3i( -1,  0,  1 ),
//		Vector3i( -1,  0,  0 ),
//		Vector3i( -1,  0, -1 ),
//		Vector3i( -1, -1,  1 ),
//		Vector3i( -1, -1,  0 ),
//		Vector3i( -1, -1, -1 ),
//
//	};
//
//	Vector3i cellPos;
////	GRIDCELL grid;
//
//	for (dw::int32 indexZ = 0; indexZ < size; ++indexZ)
//	{
//		cellPos.z = indexZ + origin.z;
//		for( dw::int32 indexY = 0; indexY < size; ++indexY)
//		{
//			cellPos.y = indexY + origin.y;
//			for (dw::int32 indexX = 0; indexX < size; ++indexX)
//			{
//				cellPos.x = indexX + origin.x;
//
//				cache.getCell(cellPos, cell);
//
//				const Material& material = worldManager.getPlanet()->getMaterialLibrary().getMaterialFromId(cell.getMaterialId());
//
//				if (!material.isVoid())
//				{
//					for (dw::int32 n = 0; n < 3*3*3 - 1; ++n)
//					{
//						cache.getCell(cellPos + Neighbours[(WorldGridManager::Neighbor)n], neighbour);
//
//						if (!material.isVoid())
//						{
//							/*pour chaque sommet voir si j'ai une touche*/
//							for (dw::int32 n = 0; n < 8; ++n) 
//							{
//								//fill the grid helper
//
//								cache.getCell(cellPos + Neighbours[(WorldGridManager::Neighbor)n], neighbour);
//
//								const Material& neighbourMat = worldManager.getPlanet()->getMaterialLibrary().getMaterialFromId(neighbour.getMaterialId());
//
//								if (neighbourMat.isVoid())
//								{
//							//		addFace(float3(indexX,indexY,indexZ),cell.getMaterialId(), (WorldGridManager::Neighbor)n);
//								}
//							}
//						}
//					}
//				}
//			}
//		}
//	}
//
//	if (curindex > 0)
//	{
//		m_indexBuffer = renderer.createIndexBuffer(
//			dw::graphics::IndexBuffer::update_never,
//			dw::graphics::IndexBuffer::uint16);
//
//		m_indexBuffer->bind();
//		m_indexBuffer->resize(curindex);
//		m_indexBuffer->write(indices);
//		m_indexBuffer->unbind();
//
//		m_vertexBuffer = renderer.createVertexBuffer(
//			dw::graphics::VertexBuffer::update_never,
//			m_vertexFormat);
//
//		m_vertexBuffer->bind();
//		m_vertexBuffer->resize(curvertex);
//		m_vertexBuffer->write(vertices);
//		m_vertexBuffer->unbind();
//	}
//
//	delete vertices;
//	delete indices;
//}

	}//namespace render
} // namespace infinity

